package com.danger.Activity.Account;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.danger.Activity.BackHandledFragment;
import com.danger.Activity.R;
import com.danger.Presenter.ForgetPasswordPresenter;
import com.danger.Presenter.ForgetPasswordPresenterImpl;
import com.danger.View.ForgetPasswordView;

/**
 * Created by danger on 2016/4/24.
 */
public class ForgetPasswordFragment extends BackHandledFragment implements ForgetPasswordView, View.OnClickListener {

    private boolean hadIntercept;
    private View view;
    private EditText forget_password_email_editText;
    private Button forget_password_check_button;
    private ProgressDialog dialog;
    private ForgetPasswordPresenter presenter;
    private Toolbar forget_password_toolbar;
    private AccountActivity activity;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_forget_password, container, false);

        initView();
        initEvent();

        return view;
    }

    private void initView() {
        activity = (AccountActivity) getActivity();
        forget_password_toolbar = activity.getToolbar();
        forget_password_toolbar.setTitle("");
        activity.getToolbarTitle().setText("忘记密码");
        forget_password_toolbar.setNavigationIcon(R.mipmap.back);
        activity.setSupportActionBar(forget_password_toolbar);
        activity.getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
        dialog = new ProgressDialog(activity);
        dialog.setMessage("正在发送邮箱验证...");
        presenter = new ForgetPasswordPresenterImpl(activity, this);
        forget_password_email_editText = (EditText) view.findViewById(R.id.forget_password_email_editText);
        forget_password_check_button = (Button) view.findViewById(R.id.forget_password_check_button);
    }

    private void initEvent() {
        forget_password_check_button.setOnClickListener(this);
        forget_password_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });
    }

    @Override
    public void showDialog() {
        dialog.show();
    }

    @Override
    public void hideDialog() {
        dialog.hide();
        dialog.dismiss();
    }

    @Override
    public void showSuccess() {
        Toast.makeText(activity, "重置密码请求成功，请到" + forget_password_email_editText.getText().toString() + "邮箱进行密码重置操作", Toast.LENGTH_SHORT).show();
        getFragmentManager().beginTransaction().addToBackStack(null).replace(R.id.account_container, new LoginFragment()).commit();
    }

    @Override
    public void showError(String error) {
        Toast.makeText(activity, "重置密码失败:" + error, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_password_check_button:
                presenter.verifyByEmail(forget_password_email_editText.getText().toString());
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onBackPressed() {
        if (hadIntercept) {
            return false;
        } else {
            hadIntercept = true;
            return true;
        }
    }

}
